package me.xinping.record.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.view_recorder.*
import me.xinping.libs.base.BaseActivity
import me.xinping.libs.utils.FileUtils
import me.xinping.libs.utils.TimeUtils
import me.xinping.record.R
import me.xinping.record.controllers.MainActivityController
import me.xinping.record.ui.adapter.RecorderAdapter
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

class MainActivity : BaseActivity() {
    private lateinit var controller: MainActivityController
    private lateinit var recorderAdapter: RecorderAdapter

    var isRecording: Boolean by Delegates.observable(false) { _: KProperty<*>, _: Boolean, recording: Boolean ->
        tv_recording.text = getString(if (recording) R.string.app_recording else R.string.app_click_record)
        imv_record.setImageResource(if (recording) R.drawable.shape_red_circle else R.drawable.shape_blue_circle)
        if (!recording) {
            tv_time.text = getString(R.string.default_time_zero)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        controller = MainActivityController(this)

        initTitle()
        initView()
        addListener()
    }

    private fun initTitle() {
        title_bar.setCenterTitle(getString(R.string.app_recorder))
        title_bar.setOnRightClickListener { recorderAdapter.edit() }
    }

    private fun initView() {
        rv_recorder.layoutManager = LinearLayoutManager(this@MainActivity)
        recorderAdapter = RecorderAdapter(this@MainActivity)
        rv_recorder.adapter = recorderAdapter
    }

    private fun addListener() {
        imv_record.setOnClickListener {
            if (isRecording) {
                controller.stopRecord()
                controller.loadRecorderFileItems()
            } else {
                controller.startRecord()
            }
        }
        recorderAdapter.playClickAction = { filePath ->
            controller.playRecordFile(filePath)
        }
        recorderAdapter.itemCheckAction = { _, _ ->
            val items = recorderAdapter.selectedItems()
            title_bar.setRightText(if (items.isNotEmpty()) R.string.delete else R.string.edit)
        }
        title_bar.setOnRightClickListener {
            val items = recorderAdapter.selectedItems()
            if (items.isNotEmpty()) {
                //删除前先停止播放
                controller.stopPlay()
                //删除并重新加载
                items.forEach { FileUtils.deleteRecorderFile(it.filePath) }
                controller.loadRecorderFileItems()
            } else {
                //编辑模式切换
                recorderAdapter.edit()
                //编辑时暂停播放
            }
        }
    }

    /**
     * 更新录音时间
     */
    fun updateRecordTime(seconds: Int) {
        tv_time.text = TimeUtils.seconds2Time(seconds)
    }

    /**
     * 更新items数据源
     */
    fun updateItems(items: ArrayList<RecorderAdapter.RecorderItem>) {
        title_bar.setRightText(if (items.size > 0) R.string.edit else 0)
        recorderAdapter.updateItems(items)
    }

    /**
     * 更新item播放状态
     */
    fun updateItemPlay(filePath: String?, isPlaying: Boolean, progress: Int = -1) {
        val findItem = recorderAdapter.findItemByFilePath(filePath)
        findItem?.let {
            it.isPlaying = isPlaying
            if (isPlaying && progress != -1) {
                it.playProgress = TimeUtils.seconds2Time(progress)
            }
            if (!isPlaying) {
                it.playProgress = null
            }
            recorderAdapter.updateItem(it)
        }
    }

    override fun onStart() {
        super.onStart()
        controller.loadRecorderFileItems()
    }

    override fun onStop() {
        super.onStop()
        controller.releaseAll()
    }
}
